package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author hwt
 * @since 2022-08-13
 */
@Api(tags = {"讲师管理"})
@RestController
@RequestMapping("eduservice/edu-teacher")
@CrossOrigin  // 解决跨域问题
public class EduTeacherController {

    @Resource
    EduTeacherService teacherService;

    @ApiOperation("所有讲师信息")
    @GetMapping("findAll")
    public R findAllTeacher() {
        return R.ok().data("items", teacherService.listObjs(null));
    }

    @ApiOperation("逻辑删除讲师信息")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable("id") String id) {
        return teacherService.removeById(id) ? R.ok() : R.error();
    }

    @ApiOperation("讲师分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") long current, @PathVariable("limit") long limit) {
        // 创建分页对象
        // current 当前页    limit 每页条数
        Page<EduTeacher> page = new Page<>(current, limit);
        // 进行分页
        // 没有分页条件，所以为 null
        teacherService.page(page, null);
        // 总记录数
        long total = page.getTotal();
        // 当前页的数据
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation("讲师条件分页查询")
    @PostMapping(value = "pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) TeacherQuery query) {

        Page<EduTeacher> page = new Page<>(current, limit);


        // 进行条件构建
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        if (query!=null) {

            if (!StringUtils.isNullOrEmpty(query.getName()))
                wrapper.like("name", query.getName());

            if (null != query.getLevel())
                wrapper.eq("level", query.getLevel());

            if (!StringUtils.isNullOrEmpty(query.getBegin()))
                wrapper.ge("gmt_create", query.getBegin());

            if (!StringUtils.isNullOrEmpty(query.getEnd()))
                wrapper.le("gmt_create", query.getEnd());

                // 根据添加时间排序
                wrapper.orderByDesc (  "gmt_create") ;

        }


        teacherService.page(page, wrapper);
        // 总记录数
        long total = page.getTotal();
        // 当前页的数据
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation("添加讲师接口")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher Teacher) {
        return teacherService.save(Teacher) ? R.ok() : R.error();
    }

    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id) {

        EduTeacher byId = teacherService.getById(id);

        return null != byId ? R.ok().data("teacher", byId) : R.error();
    }

    @ApiOperation("根据id修改讲师")
    @PutMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "teacher",value = "要修改的讲师信息对象",required = true)
            @RequestBody EduTeacher teacher) {

        return teacherService.updateById(teacher) ? R.ok() : R.error();
    }


}

